package com.canyou.redmine.ui.base;

import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.sdsmdg.tastytoast.TastyToast;

/**
 * 基类
 *
 * @author ryk
 */
public class BaseActivity extends AppCompatActivity {

    //网络加载提示
    private KProgressHUD hud;
    protected Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
    }

    /**
     * 是否显示返回按钮
     *
     * @param show
     */
    public void setBackButton(boolean show) {
        if (show) {
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setElevation(0);
        }

    }

    /**
     * activity返回事件
     *
     * @return
     */
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    /**
     * 消息提示
     *
     * @param resId 文字id
     * @param type  消息类型（如：TastyToast.SUCCESS）
     */
    public void AlertToast(int resId, int type) {
        TastyToast.makeText(getApplicationContext(), getString(resId), TastyToast.LENGTH_SHORT, type);
    }

    /**
     * 消息提示
     *
     * @param message 文字内容
     * @param type    消息类型（如：TastyToast.SUCCESS）
     */
    public void AlertToast(String message, int type) {
        TastyToast.makeText(getApplicationContext(), message, TastyToast.LENGTH_SHORT, type);
    }

    /**
     * 加载提示（含提示文字）
     *
     * @param message
     */
    protected void showHUD(String message) {
        if (hud == null) {
            hud = KProgressHUD.create(getApplicationContext())
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
        }
        hud.setLabel(message);
        hud.show();

    }

    /**
     * 加载提示
     */
    protected void showHUD() {
        if (hud == null) {
            hud = KProgressHUD.create(context)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
        }
        hud.show();

    }

    /**
     * 关闭加载动画
     */
    protected void closeHUD() {
        if (hud != null) {
            hud.dismiss();
            hud = null;
        }
    }
}
